import axios from 'axios'
// import store from '@/store/index'
// import {encrypt,decrypt} from "./sm4Util"
// http://192.168.20.18:9098/
const _1819098 = 'http://localhost:12828/';
//http://121.199.12.46:8080'  测试服务器
// const _1819098 = 'http://192.168.20.22:9098';
let base = _1819098
// 192.168.0.203:9090  正式服务器
// let base = 'http://' +  window.location.host + '/ylty'
import {message,Modal} from "ant-design-vue";
// import {myDecrypt} from '@/untils/cryptoJS';
// http://192.168.20.3:9090/ 张
// 'http://192.168.20.22:9098/' 谢
// 4:9090 张
// ttp://192.168.20.116:8080/';
const baseAxios = axios.create({
    baseURL: base,
    withCredentials: false,
    timeout: 10000000000,
})

// 添加请求拦截器
baseAxios.interceptors.request.use(
    config => {
        config.headers['Access-Control-Allow-Origin'] = '*'
        // config.headers.Authorization = localStorage.getItem('Token')
        config.headers.Authorization = sessionStorage.getItem('istoken')
        config.headers['Access-Control-Allow-Credentials'] = true
/*        config.headers['Content-Type'] = 'multipart/form-data'
        config.headers['Content-Type']   = 'application/json'*/
        // config.headers['Content-Type'] = 'multipart/form-data'
        config.headers['Content-Type']  = 'application/json'
        // 和加密冲突 需要解决办法
        config.headers['Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild'];
         // if(config.data){
         //     const sm4 = new SM4Util();
         //     config.data = sm4.encryptDefault_CBC(JSON.stringify(config.data) )
         // }
        // 2025-02-13  加密方式不可删除
        // encrypt
        return config
    },
    function (error) {
        // 对请求错误做些什么
        // Store.commit('setOverall', false)
        return Promise.reject(error)
    }
)
// 添加响应拦截器
let num = 0
baseAxios.interceptors.response.use(
    response => {

        if ((sessionStorage.getItem('token') == 'undefined' || sessionStorage.getItem('token') == null)) sessionStorage.setItem('token', response.headers.authorization)
        if (response.headers.authorization) {
            sessionStorage.setItem('istoken', response.headers.authorization)
        }
        if(response.headers.updatedAuthorization){
            sessionStorage.setItem('istoken', response.headers.authorization)
        }
        if (response.data.code ==  15) {
            if(num == 0){
                num = 1
                Modal.confirm({
                    title: response.data.msg,
                    onOk() {
                        if(response.data.msg){

                            message.warning(response.data.msg)
                        }
                        sessionStorage.clear();
                        window.location.href = "/";

                    },
                    onCancel() {
                        if(response.data.msg){

                            message.warning(response.data.msg)
                        }
                        sessionStorage.clear();
                        window.location.href = "/";
                    },
                    class: 'test',
                });
            }


            //////.log(response.data)
        }else

        if (response.data.code == 2 || response.data.code == 3) {
            ////////.log('访问出错401001')
            if(response.data.msg){

                message.warning(response.data.msg)
            }
            sessionStorage.clear();
            window.location.href = "/";
            return false
        } else if (response.data.code != 0 && response.data.code != 500 && response.config.responseType !=  'blob') {
            if(response.data.msg){

                message.warning(response.data.msg)
            }
            //////.log(response.data)
        }
        function responseFun() {
            sessionStorage.clear();
            window.location.href = "/";
        }
        if (response.data) {
            if (response.data.code == 500) {

                if(response.data.msg){

                    message.warning(response.data.msg)
                }
                store._state.data.isSpin = false
                // return response.data
            }
        } else {
            //alert('网络异常!请稍后再试')
        }

   // if(response.data.data){
   //     const sm4 = new SM4Util();
   //     response.data.data = sm4.decryptDefault_CBC(response.data.data)
   //     console.log(  response.data.data)
   // }
   // 2025-02-13  解密方式不可删除
         return response.data
        // eslint-disable-next-line no-undef
    }, error => {

        setTimeout(() => {
            // this.Hint({
            //     message: '出错了，稍后刷新重试一下',
            //     type: 'warning'
            // });
        }, 500)
        return Promise.reject(error);
    }
)

export default baseAxios
